<template>
	<view class="container">
		<view class="cu-form-group">
			<view class="title">题目</view>
			<input placeholder="请输入题目" name="input" v-model="question"></input>
		</view>
		<view class="cu-form-group">
			<view class="title">答案</view>
			<input placeholder="请输入答案" name="input" v-model="answer"></input>
		</view>
		<view class="padding-xl">
			<button class="cu-btn block bg-green margin-tb-sm lg" @click="save">
				<text class="cuIconfont-spin"></text>保存</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				question: '',
				answer: ''
			};
		},
		methods: {

			save() {
				if (this.question === '') {
					uni.showToast({
						title: '请输入题目',
						icon: 'none',
						mask: true,
						duration: 1500
					});
					return
				}
				if (this.answer === '') {
					uni.showToast({
						title: '请输入答案',
						icon: 'none',
						mask: true,
						duration: 1500
					});
					return
				}
				let item = {
					question: this.question,
					answer: this.answer
				}
				try {
					const value = uni.getStorageSync('topicList');
					if (value) {
						console.log(value);
						let parse = JSON.parse(value)
						parse.push(item)
						uni.setStorageSync('topicList', JSON.stringify(parse));
					} else {
						uni.setStorageSync('topicList', JSON.stringify([item]));
					}
					uni.showToast({
						title: '添加成功',
						mask: true,
						duration: 1000
					});
					this.question = ''
					this.answer = ''
				} catch (e) {
					// error
				}
			}
		}
	}
</script>

<style lang="less" scoped>

</style>
